import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import NBT.MapChunk;


public class LocationChooser extends JPanel {

	private JTextField x_pos,z_pos;
	private ChunkPreview preview;
	
	MinecraftMap map=null;
	
	public void setLocation(int x, int z)
	{
		x_pos.setText(""+x);
		z_pos.setText(""+z);
	}
	
	public LocationChooser()
	{				
		
		setBorder(BorderFactory.createTitledBorder("Choose destination chunk: "));
		
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		JPanel top=new JPanel();
		
		JLabel xlabel=new JLabel("X: ");
		x_pos=new JTextField("0");
		x_pos.setPreferredSize(new Dimension(40,20));
		top.add(xlabel);
		top.add(x_pos);			
		
		JLabel zlabel=new JLabel("Z: ");
		z_pos=new JTextField("0");
		z_pos.setPreferredSize(new Dimension(40,20));
		top.add(zlabel);
		top.add(z_pos);
		
		JButton refresh=new JButton("Refresh");
		top.add(refresh);
				
		refresh.addActionListener(new AbstractAction() {		
			public void actionPerformed(ActionEvent arg0) {
				refresh(0,0);
			}
		});
		
		preview=new ChunkPreview();
		
		JButton move_up=new JButton("U");
		JButton move_down=new JButton("D");
		JButton move_left=new JButton("L");
		JButton move_right=new JButton("R");
		
		move_up.addActionListener(new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				refresh(0,-1);
			}
		});
		move_down.addActionListener(new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				refresh(0,1);
			}
		});
		move_left.addActionListener(new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				refresh(-1,0);
			}
		});
		move_right.addActionListener(new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				refresh(1,0);
			}
		});

		
		JPanel preview_panel=new JPanel();		
		preview_panel.setLayout(null);		
		preview_panel.add(move_up);
		preview_panel.add(move_left);
		preview_panel.add(preview);				
		preview_panel.add(move_right);
		preview_panel.add(move_down);
		
		preview_panel.setPreferredSize(new Dimension(228,188));
		preview.setBounds(50, 30, 128, 128);
		move_up.setBounds(50,0,128,30);
		move_down.setBounds(50,158,128,30);
		move_left.setBounds(0,30,50,128);
		move_right.setBounds(178,30,50,128);
		

		
		add(top);		
		add(preview_panel);
	}
	
	void refresh(int delta_x, int delta_z)
	{
		int x,z;
		try{
			x=Integer.parseInt(x_pos.getText())+delta_x;
			z=Integer.parseInt(z_pos.getText())+delta_z;
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Number format error: "+e.getMessage());
			return;
		}
		
		x_pos.setText(""+x);
		z_pos.setText(""+z);
		
		if(Main.mainFrame.minecraft_map==null)
		{
			JOptionPane.showMessageDialog(null, "You need to load the world save first!");
			return;
		}
		
		Main.mainFrame.minecraft_map.clearChunks();
		
		boolean b=false,ret;
		for(int i=-1; i<=1; i++)
			for(int j=-1; j<=1; j++)
			{
				ret=Main.mainFrame.minecraft_map.loadChunk(x+i, z+j);
				b=b||ret;
			}
		
		if(b)
		{
			preview.setImage(Main.mainFrame.minecraft_map.render());
		}
		else
		{
			preview.setImage(null);					
		}
		
		preview.repaint();
	}
	
	public MapChunk getSelectedChunk()
	{
		int x,z;
		try{
			x=Integer.parseInt(x_pos.getText());
			z=Integer.parseInt(z_pos.getText());
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Number format error: "+e.getMessage());
			return null;
		}
		
		if(Main.mainFrame.minecraft_map==null)
		{
			JOptionPane.showMessageDialog(null, "You need to load the world save first!");
			return null;
		}
		
		return Main.mainFrame.minecraft_map.getChunk(x, z);
	}
	
	public MapChunk getChunk(int x, int z)
	{
		if(Main.mainFrame.minecraft_map==null)
		{
			JOptionPane.showMessageDialog(null, "You need to load the world save first!");
			return null;
		}
		
		return Main.mainFrame.minecraft_map.getChunk(x, z);
	}
	
	public File getSelectedChunkPath()
	{
		int x,z;
		try{
			x=Integer.parseInt(x_pos.getText());
			z=Integer.parseInt(z_pos.getText());
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Number format error: "+e.getMessage());
			return null;
		}
		
		if(Main.mainFrame.minecraft_map==null)
		{
			JOptionPane.showMessageDialog(null, "You need to load the world save first!");
			return null;
		}
		
		File path=null;
		try {
			path=Main.mainFrame.minecraft_map.getChunkPath(x, z);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Cannot find chunk:"+e.getMessage());
			return null;
		}
		
		return path;
	}
	
	public File getChunkPath(int x, int z)
	{		
		if(Main.mainFrame.minecraft_map==null)
		{
			JOptionPane.showMessageDialog(null, "You need to load the world save first!");
			return null;
		}
		
		File path=null;
		try {
			path=Main.mainFrame.minecraft_map.getChunkPath(x, z);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Cannot find chunk:"+e.getMessage());
			return null;
		}
		
		return path;
	}
}
